import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter);

import UserManage from "../components/views/UserManager";
import Seller from "../components/views/Seller";
import Category from "../components/views/Category";
import Food from "../components/views/Food";
import FoodUpdate from "../components/views/FoodUpdate";
import Order from "../components/views/Order";
import LoginManager from "../components/views/LoginManager";
import RestePwd from "../components/views/RestePwd";
import UploadFile from "../components/views/UploadFile";

const routes = [
  {
    path: '/user',
    name: 'user',
    component: UserManage
  },
  {
    path: '/food',
    name: 'food',
    component: Food
  },
  {
    path: '/seller',
    name: 'seller',
    component: Seller
  },
  {
    path: '/category',
    name: 'category',
    component: Category
  },
  {
    path: '/foodUpdate',
    name: 'foodUpdate',
    component: FoodUpdate
  },
  {
    path: '/order',
    name: 'Order',
    component: Order
  },
  {
    path: '/login',
    name: 'LoginManager',
    component: LoginManager
  },
  {
    path: '/resetPwd',
    name: 'RestePwd',
    component: RestePwd
  },
  {
    path: '/upload',
    name: 'UploadFile',
    component: UploadFile
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {

  if (to.path === '/login') {

    next();

  } else {

    let token = localStorage.getItem('adtoken');
    console.log(token)
    console.log('拦截到token了')
    // if (next('/register')){
    //   next();
    // }

    if (token === 'null' || token === '') {

      next('/login');

    } else {

      next();

    }

  }

});

export default router
